package mortal.game.control
{
    import Message.Public.*;
    import modules.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.info.*;
    import mortal.game.view.pack.*;
    import mortal.game.view.treasure.*;
    import mortal.mvc.core.*;

    public class TreasureWareHouseController extends Controller
    {
        private var _treasureWarehourse:TreasureWarehourseModule;
        private var _treasureGetItem:TreasureGetItemPanel;
        private var _curItemObj:Object;

        public function TreasureWareHouseController()
        {
            this._curItemObj = {};
            
        }

        public function get treasureWarehourse() : TreasureWarehourseModule
        {
            if (this._treasureWarehourse == null)
            {
                this._treasureWarehourse = new TreasureWarehourseModule();
            }
            return this._treasureWarehourse;
        }

        public function get treasureGetItem() : TreasureGetItemPanel
        {
            if (this._treasureGetItem == null)
            {
                this._treasureGetItem = new TreasureGetItemPanel();
            }
            return this._treasureGetItem;
        }

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.TreasureWarehourseModuleOpen, this.onFairylandWarehourseHandler);
            Dispatcher.addEventListener(EventName.TreasureUseProp, this.onFairylandUsePropHandler);
            Dispatcher.addEventListener(EventName.TreasureMoveToBag, this.onTreasureMoveToBagHandler);
            Dispatcher.addEventListener(EventName.TreasureMoveAllToBag, this.onTreasureMoveAllToBagHandler);
            Dispatcher.addEventListener(EventName.TreasureGetItemWindowShow, this.onGetItemWindowShowHandler);
            NetDispatcher.addCmdListener(ServerCommand.BackPackTreasurePackChange, this.onTreasurePackChangeHandler);
            NetDispatcher.addCmdListener(ServerCommand.BackPackTreasureGetItemsChange, this.onTreasureGetItemsChangeHandler);
            
        }

        private function onFairylandWarehourseHandler(event:DataEvent) : void
        {
            if (this.treasureWarehourse.isHide)
            {
                this.treasureWarehourse.show();
            }
            else
            {
                this.treasureWarehourse.hide();
            }
            
        }

        private function onFairylandUsePropHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as ItemData;
            if (_loc_2 != null)
            {
                GameProxy.packProxy.useLottery(_loc_2.data.uid, 1);
            }
            
        }

        private function onTreasureMoveToBagHandler(event:DataEvent) : void
        {
            var _loc_11:int = 0;
            var _loc_12:DragDropData = null;
            var _loc_2:* = event.data as ItemData;
            var _loc_3:* = _loc_2.data.uid;
            var _loc_4:* = EPlayerItemPosType._EPlayerItemPosTypeTreasure;
            var _loc_5:* = EPlayerItemPosType._EPlayerItemPosTypeBag;
            var _loc_6:* = this._treasureWarehourse.itemListPanel.selectedIndex;
            var _loc_7:* = cache.pack.backPackCache.itemLength;
            var _loc_8:* = cache.pack.backPackCache.getAllItems();
            var _loc_9:Boolean = false;
            var _loc_10:int = 0;
            while (_loc_10 < _loc_8.length)
            {
                
                if (!cache.pack.backPackCache.getItemDataByIndex((_loc_10 + 1)))
                {
                    _loc_11 = cache.pack.treasurePackCache.getIndexByUid(_loc_3);
                    _loc_12 = new DragDropData(_loc_4, _loc_5, _loc_3, _loc_11, (_loc_10 + 1), _loc_2, null);
                    GameProxy.packProxy.move(_loc_12);
                    _loc_9 = true;
                    
                }
                _loc_10++;
            }
            if (!_loc_9)
            {
                MsgManager.addBroadCast("背包空间不足，请整理背包");
            }
            
        }

        private function onTreasurePackChangeHandler(param1:Object) : void
        {
            if (this._treasureWarehourse != null)
            {
                this._treasureWarehourse.updateModule();
            }
            
        }

        private function onTreasureGetItemsChangeHandler(param1:Object) : void
        {
            if (param1)
            {
                this._curItemObj = param1;
            }
            if (this._treasureWarehourse != null)
            {
                this._treasureWarehourse.updateModule();
            }
            
        }

        private function onGetItemWindowShowHandler(event:DataEvent) : void
        {
            
        }

        private function onTreasureMoveAllToBagHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as Array;
            if (_loc_2.length == 0)
            {
                GameProxy.packProxy.moveBag(EPlayerItemPosType._EPlayerItemPosTypeTreasure, EPlayerItemPosType._EPlayerItemPosTypeBag);
            }
            else
            {
                GameProxy.packProxy.moveBag(EPlayerItemPosType._EPlayerItemPosTypeTreasure, EPlayerItemPosType._EPlayerItemPosTypeBag, _loc_2);
            }
            
        }

    }
}
